#ifndef __GAUTHENTICATE_H__
#define __GAUTHENTICATE_H__

#include <curl/curl.h>

static const char GCLOUDP_DELIMITER[] = "%40";
static const char GCLOUDAUTH_URL[] = "https://www.google.com/accounts/ClientLogin";
#define GCLOUDP_URL "http://www.google.com/cloudprint"


/* Google 'pages' results in a range pages of 25 entries. But for downloading
 * all results its requirement to set a 'good enough' upper limit of range of
 * entries. A hack to make 'gcal_dump' work.
 */
static const char GCAL_UPPER[] = "max-results=999999999";

static const int GCAL_DEFAULT_ANSWER = 200;
static const int GCAL_REDIRECT_ANSWER = 302;
static const int GCAL_EDIT_ANSWER = 201;
static const int GCAL_CONFLICT = 409;

static const char ACCOUNT_TYPE[] = "accountType=HOSTED_OR_GOOGLE";
static const char EMAIL_FIELD[] = "Email=";
static const char PASSWD_FIELD[] = "Passwd=";
static const char SERVICE_FIELD[] = "service=";
static const char CLIENT_SOURCE[] = "source=libgcal";
static const char HEADER_AUTH[] = "Auth=";
static const char HEADER_GET[] = "Authorization: GoogleLogin auth=";

typedef char dom_document;

/** Library structure. It holds resources (curl, buffer, etc).  */
typedef struct gservice_t {
	/** Memory buffer */
	char *buffer;
	/** Its length */
	size_t length;
	/** previous length, required when downloading binary data
	 * i.e. contact photo data
	 */
	size_t previous_length;
	/** gcalendar authorization */
	char *auth;
	/** curl data structure */
	CURL *curl;
	/** Atom feed URL */
	char *url;
	/** The user name */
	char *user;
        /** The domain */
        char *domain;
	/** DOM xml tree (an abstract type so I can plug another xml parser) */
	dom_document *document; 
	/** A flag to control if the buffer has XML atom stream */
	char has_xml;
	/** Google service choose, currently Calendar and contacts  */
	char *service;
	/** HTTP code status from last request */
	long http_code;
	/** CURL error messages */
	char *curl_msg;
	/** Internal status from last request */
	int internal_status;
	/** Handler to internal logging file */
	FILE *fout_log;
	/** Max number of results (google pages its query results) */
	char *max_results;
	/** User defined timezone in RFC 3339 format: -/+hh:mm:ss */
	char *timezone;
	/** User defined location (used to define in which timezone the
	 * results will be returned). The format is
	 * "Continent/City_without_spaces", e.g. "America/Los_Angeles".
	 */
	char *location;
	/** Control if deleted entries will be returned or not (only
	 * valid for google contacts.
	 */
	int deleted;
	/** Controls if raw XML entries will be stored inside each
	 * event/contact object.
	 */
	char store_xml_entry;

	char *username;
	char *password;
	char *xmpp_auth;
	char *proxy;
} gservice;

#define BOUNDARY  "ABSYNTHASI44199101aaakkaddd"

extern int gcal_get_authentication(gservice *gcalobj, char *user, char *password);
extern gservice *gcal_construct(int service);
extern int http_post(gservice *gcalobj, const char *url, char *header, char *header2, char *header3, char *header4,
	      char *post_data, unsigned int length, const int expected_answer, const char *gdata_version);
char *encode_multipart(char *buffer, ...);
int http_post_auth(gservice *svc, const char *url, char *post_data, unsigned int length, ...);
int get_url(gservice *gs, char* url, char *tokens[], struct curl_httppost *data, int anon);
int get_file(gservice *gs, char* url, char* filePath, int anon);
#endif
